﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Drawing;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Framework;

namespace PlanningGIS.TocContextMenu.Button
{
     /// <summary>
    /// 作用：TOC右键菜单取消图层分组按钮类
    /// 作者：
    /// 编写日期:2008-08-01
    internal  class CancelGroupLayer:BaseCommand 
    {  
        IApplication m_Application = null;
        public CancelGroupLayer()
        {
            base.m_caption = "取消图层分组";
            base.m_toolTip = base.Caption; 
        }
     
        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }
        public override void OnClick()
        {       
            IGroupLayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as IGroupLayer;
            ICompositeLayer pCompoLayer = pLayer as ICompositeLayer;
            if (pCompoLayer == null) return;
            if (pCompoLayer.Count == 0) return;
            IGroupLayer pGroupLayer = GetParentLayer( (m_Application.Document as IMxDocument).ActiveView.FocusMap, pLayer);
            if (pGroupLayer == null)
            {
                 for(int i=0;i<pCompoLayer.Count;i++)
                {
                     (m_Application.Document as IMxDocument).ActiveView.FocusMap.AddLayer(pCompoLayer.get_Layer(i));
                }
            }
            else
            {
                for(int i=0;i<pCompoLayer.Count;i++)
                {
                    pGroupLayer.Add(pCompoLayer.get_Layer(i));
                }
            }
            ((IGroupLayer)pLayer).Clear();
             (m_Application.Document as IMxDocument).ActiveView.FocusMap.DeleteLayer(pLayer);
             (m_Application as IFramework).TOCControl.Update();
        }
   
        /// <summary>
        /// 如果一个图层属于一个图层组，返回它所在的图层组，否则返回null
        /// </summary>
        /// <param name="pLayer"></param>
        /// <returns></returns>
        private IGroupLayer GetParentLayer(IMap   pMap, ILayer pBaseLayer)
        {
            int count = pMap.LayerCount;
            ILayer pLayer;
            IGroupLayer pGroupLayer;
            for (int i = 0; i < count; i++)
            {
                pLayer = pMap.get_Layer(i);
                if (pLayer.Valid && pLayer is IGroupLayer)
                {
                    pGroupLayer = GetParentLayer(pLayer as IGroupLayer, pBaseLayer);
                    if (pGroupLayer != null) return pGroupLayer;
                }
            }
            return null;
        }

        private IGroupLayer GetParentLayer(IGroupLayer pGroupLayer, ILayer pBaseLayer)
        {
            ICompositeLayer pCompositeLayer = pGroupLayer as ICompositeLayer;
            for (int i = 0; i < pCompositeLayer.Count; i++)
            {
                if (pCompositeLayer.get_Layer(i) == pBaseLayer) return pGroupLayer;
                if (pCompositeLayer.get_Layer(i).Valid && pCompositeLayer.get_Layer(i) is IGroupLayer)
                {
                    return GetParentLayer(pCompositeLayer.get_Layer(i) as IGroupLayer, pBaseLayer);
                }
            }
            return null;
        }
        public override bool Enabled
        {
            get
            {
                return ((m_Application as IFramework).IsMapCtrlactive && (m_Application as IFramework).TOCControl.CustomProperty is IGroupLayer) ? true : false;
            }
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
    
}
